package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-11-14
 */
@Api(tags = "优惠卷活动")
@RestController
@RequestMapping("/admin/activity/couponInfo")
public class CouponInfoController {

    @Resource
    private CouponInfoService couponInfoService;

    // 分页查询
    @ApiOperation("分页查询")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit) {
        IPage<CouponInfo> pageModel = couponInfoService.selectedPage(page, limit);
        return Result.ok(pageModel);
    }

    // 根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        CouponInfo couponInfo = couponInfoService.getCouponInfoById(id);
        return Result.ok(couponInfo);
    }

    // 添加优惠卷
    @ApiOperation("添加优惠卷")
    @PostMapping("save")
    public Result save(@RequestBody CouponInfo couponInfo) {
        boolean save = couponInfoService.save(couponInfo);
        return save ? Result.ok(null) : Result.fail(null);
    }

    // 修改优惠卷
    @ApiOperation("修改优惠卷")
    @PutMapping("update")
    public Result update(@RequestBody CouponInfo couponInfo) {
        boolean update = couponInfoService.updateById(couponInfo);
        return update ? Result.ok(null) : Result.fail(null);
    }

    // 删除优惠卷
    @ApiOperation("删除优惠卷")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean remove = couponInfoService.removeById(id);
        return remove ? Result.ok(null) : Result.fail(null);
    }

    // 批量删除优惠卷
    @ApiOperation("批量删除优惠卷")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean remove = couponInfoService.removeByIds(idList);
        return remove ? Result.ok(null) : Result.fail(null);
    }

    // 根据id获取优惠卷规则数据
    @ApiOperation("根据id获取优惠卷规则数据")
    @GetMapping("findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable Long id) {
        Map<String, Object> map = couponInfoService.findCouponRuleList(id);
        return Result.ok(map);
    }

    // 添加优惠卷规则数据
    @ApiOperation("添加优惠卷规则数据")
    @PostMapping("saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }

}

